package com.example.anydo.util.pub_util;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;

import com.example.anydo.constant.Music;

public class MusicPlayer {
    private String[] musicSet = Music.musicSet;
    private MediaPlayer player;
    private boolean isPause = false;
    private Activity activity;

    public MusicPlayer(Activity activity){
        this.activity = activity;
        player = new MediaPlayer();

    }
    public void play(int pos){

        stop();

        try {
            AssetFileDescriptor asset = activity.getAssets().openFd(musicSet[pos]);

            player.setDataSource(
                    asset.getFileDescriptor(),
                    asset.getStartOffset(),
                    asset.getLength());

            player.prepare();

        } catch (Exception e) {
            e.printStackTrace();
        }
        player.start();
    }

    public void pause(){
        if(player.isPlaying()) {
            player.pause();
        }
    }

    public void stop(){
        if (player.isPlaying()){
            player.reset();
        }
    }

    public void onDestroy(){
        stop();
        player.release();
    }
}
